/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dtn.wsn.middleware;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
//import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

/**
 * XMLMessageHandler to encode HttpsqsMessage into XML string 
 * and decode HttpsqsMessage from XML string
 * @author hao
 */
public class XMLMessageHandler extends DefaultHandler {

    private HttpsqsMessage httpsqsMessage = null;

    public HttpsqsMessage getHttpsqsMessage() {
        return httpsqsMessage;
    }
    private String preTag = null;//
    // private static final String HTTPSQSMESSAGE_HEADER =
    // "httpsqsmessage-header";
    // private static final String HTTPSQSMESSAG_BODY = "httpsqsmessage-body";
    private static final String ElEMENT_MESSAGEID = "messageId";
    private static final String ElEMENT_MESSAG_BODY = "message_body";

    @Override
    public void startDocument() throws SAXException {
        httpsqsMessage = new HttpsqsMessage();
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        preTag = qName;
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        preTag = null;
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if (preTag != null) {
            String content = new String(ch, start, length);

            if (ElEMENT_MESSAGEID.equals(preTag)) {
                this.httpsqsMessage.setMessageId(content);
            } else if (ElEMENT_MESSAG_BODY.equals(preTag)) {
                httpsqsMessage.setMessage_body(content);
            }
        }
    }

    public HttpsqsMessage getHttpsqsMessage(String xmlstr) throws Exception {
        InputStream xmlstream = new ByteArrayInputStream(xmlstr.getBytes());
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        XMLMessageHandler handler = new XMLMessageHandler();
        parser.parse(xmlstream, handler);
        return handler.getHttpsqsMessage();
    }
    
    /**
     * decode HttpsqsMessage from a xml string
     * @param hsm
     * @return
     * @throws IOException
     * @throws JDOMException 
     */
    public String httpsqsMessage2XML(HttpsqsMessage hsm) throws IOException,
            JDOMException {
        Element root = new Element("httpsqsmessage");
        Document doc = new Document(root);
        // construct Header
        Element elementHeader = new Element("httpmessage-header");
        elementHeader.addContent(new Element("messageId").setText(hsm.getMessageId()));
        elementHeader.addContent(new Element("destination").setText(hsm.getMessageId()));
        root.addContent(elementHeader);

        // construct body
        Element elementBody = new Element("httpmessage_body");
        elementBody.addContent(new Element("message_body").setText(hsm.getMessage_body()));
        root.addContent(elementBody);
        XMLOutputter outputter = new XMLOutputter();

        return outputter.outputString(doc);

    }

    public static void main(String[] args) throws Throwable {
        XMLMessageHandler hsmhandler = new XMLMessageHandler();
        // File file = new File("message.xml");
        // InputStream input = new FileInputStream(file);
        String xmlStr = "<httpsqsmessage><httpsqsmessage-header><messageId>1212</messageId><destination>test_app_send"
                + "</destination></httpsqsmessage-header><httpsqsmessage-body><message_body>DTN_Send</message_body>"
                + "</httpsqsmessage-body></httpsqsmessage>";
        HttpsqsMessage message = hsmhandler.getHttpsqsMessage(xmlStr);
        System.out.println(message.getMessage_body());

    }
}
